#! /bin/bash
#{{ ansible_managed }}
set -x

if [ "x$1" = "x" ]; then
    echo "Restore script is expecting a restore point as parameter"
    echo "None provided. exiting"
    exit 1
fi

# Generated with Ansible vars
MDATE=$1 ## Should be %Y-%m-%d-%H-%M-%S format
blob_dir="blob-backup-${MDATE}"
backup_dir={{ nexus_backup_dir }}
data_dir={{ nexus_data_dir }}
db_restore_dir={{ nexus_db_restore_dir }}

systemctl stop nexus
return_status=$?
if [ $return_status != 0 ]; then
  echo "Error code: $return_status Failed to stop nexus service"
  exit $return_status
else
  rm -rf ${data_dir}/db/*
  rm -rf ${data_dir}/blobs/*
  sudo -u {{ nexus_os_user }} cp ${backup_dir}/${blob_dir}/db/* ${db_restore_dir}
{% for item in nexus_blobstores %}
  sudo -u {{ nexus_os_user }} cp -r ${backup_dir}/${blob_dir}/{{ item.name }} ${data_dir}/blobs/
{% endfor %}
  sudo -u {{ nexus_os_user }} cp -r ${backup_dir}/${blob_dir}/default ${data_dir}/blobs/
fi
